using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using OpenComposite.Repository.Properties;
using OpenComposite.EII.Repository;

namespace OpenComposite.EII.RepositoryUIs
{
	public partial class FormCreateNewComposite : Forms.FormCreateNewBase
	{
		#region Initialization
		protected FormCreateNewComposite()
			: base()
		{
			InitializeComponent();
		}
		public FormCreateNewComposite(Repository.ServiceModule item, bool bAllowContinue)
			: base(item, item, bAllowContinue)
		{
			InitializeComponent();

			if ( this.RepositoryItem != null ) {
				this.Text = string.Format(Resources.msgCreateNew, this.RepositoryItem.RepositoryItemTypeName);
			}
			displayCapability();
		}
		#endregion

		#region Protected Members

		#region Methods

		// override
		protected override void OnValidateNewItem(CancelEventArgs e)
		{
			DataTable dt = Global.Data.SearchRepositoryItems(
				Global.Status.CurrentDomainId, this.RepositoryItem.RepositoryItemType,
				txtName.Text, null, true);
			if ( dt != null && dt.Rows.Count > 0 ) {
				MessageBox.Show("An item with this name exists in the current domain." + Environment.NewLine + Environment.NewLine +
								"Please enter another name.",
								this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
				txtName.Focus();
				txtName.SelectAll();
				e.Cancel = true;
			} else if ( this._capability == null ) {
				MessageBox.Show("Please select an existing or create a new capability.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
				btnSelectCap.Focus();
				e.Cancel = true;
			}
			base.OnValidateNewItem(e);
		}
		protected override void OnSaveClose(CancelEventArgs e)
		{
			save(e);
			base.OnSaveClose(e);
		}
		protected override void OnSaveContinue(CancelEventArgs e)
		{
			save(e);
			base.OnSaveContinue(e);
		}

		#endregion

		#endregion

		#region Private Members

		#region Variables
		private Capability _capability = null;
		#endregion

		#region Methods

		private void save(CancelEventArgs e)
		{
			if ( txtName.Text == "" ) {
				MessageBox.Show("Please enter a name.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
				txtName.Focus();
				// cancel
				if ( e != null )
					e.Cancel = true;
				// return
				return;
			}
			ServiceModule svcmod = this.RepositoryItem as ServiceModule;
			svcmod.Name = txtName.Text;
			svcmod.Description = txtDesc.Text;

			svcmod.Capability = _capability;

			svcmod.CreatedType = OpenComposite.EII.Repository.CreatedTypeFlags.UserCreated;
			svcmod.DomainId = this.DomainID;
		}

		private void displayCapability()
		{
			txtCapability.SuspendLayout();
			if ( _capability != null ) {
				txtCapability.ForeColor = TextBox.DefaultForeColor;
				txtCapability.TextAlign = HorizontalAlignment.Left;
				txtCapability.Text = _capability.Name;
			} else {
				txtCapability.ForeColor = SystemColors.GrayText;
				txtCapability.TextAlign = HorizontalAlignment.Center;
				txtCapability.Text = "Please select a capability";
			}
			txtCapability.ResumeLayout(true);
		}
		#endregion

		#region Event Handlers
		private void btnOK_Click(object sender, EventArgs e)
		{
			CancelEventArgs cargs = new CancelEventArgs(false);
			OnValidateNewItem(cargs);
			if ( !cargs.Cancel ) {
				OnSaveClose(new CancelEventArgs(false));
			}
		}

		private void txtName_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
		{
			if ( e.KeyCode == Keys.Enter ) {
				CancelEventArgs cargs = new CancelEventArgs(false);
				OnValidateNewItem(cargs);
				if ( !cargs.Cancel ) {
					OnSaveClose(new CancelEventArgs(false));
				}
			}
		}

		private void btnSelectCap_Click(object sender, EventArgs e)
		{
			Forms.FormSelectRepositoryEx frm = new OpenComposite.EII.Forms.FormSelectRepositoryEx(this.RepositoryItem);
			frm.AllowedTypes = new Repository.ItemType[] { Repository.ItemType.Capability };
			frm.DomainID = Global.Status.CurrentDomainId;
			frm.RepositoryID = Global.Status.CurrentRepositoryId;
			frm.MultiSelect = false;
			frm.SelectedItem = _capability;
			if ( frm.ShowDialog() == DialogResult.OK ) {
				_capability = frm.SelectedItem as Capability;
				displayCapability();
			}
		}

		private void btnCreateCap_Click(object sender, EventArgs e)
		{
			IRepositoryItem parent = null;
			Capability newCap = this.RepositoryService.GetNewItem<Capability>(true, ref parent);
			if ( newCap != null ) {
				_capability = newCap;
				Global.Data.AddRepositoryItemToDomainFolder(_capability.DomainId, _capability);
				displayCapability();
			}
		}

		private void txtName_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = !Helpers.IsValidCharForName(e.KeyChar);
		}

		#endregion

		#endregion

	}
}